package org.ifilm.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.ifilm.model.City;
import org.ifilm.model.Country;
import org.ifilm.model.Language;
import org.ifilm.model.TipsAndTricks;
import org.ifilm.model.User;
import org.ifilm.model.UserType;
import org.ifilm.service.UtilService;
import org.ifilm.util.GeneralConstants;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

/**
 * 
 * Util Contoller. All requests about language, location, userType
 * 					will be here
 * 
 * @author Mustafa Genç
 * 
 */
@Controller
@RequestMapping("/util/**")
public class UtilController {
	@Autowired
	private UtilService utilService;
	
	@RequestMapping(value = "/getCountries", method = {RequestMethod.POST, RequestMethod.GET})
	public ResponseEntity<Map<String, Object>> listCountries(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Map<String, Object> responseBody = new HashMap<String, Object>();
		
		List<Country> countries = new ArrayList<Country>();
		countries = utilService.listCountries();
		responseBody.put("countryList", countries);
		
		return new ResponseEntity<Map<String, Object>>(responseBody, HttpStatus.OK);
	}
	
	@RequestMapping(value = "/getCitiesByCountryId", method = {RequestMethod.POST, RequestMethod.GET})
	public ResponseEntity<Map<String, Object>> getCities(@RequestParam(value = "countryId") Long countryId, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Map<String, Object> responseBody = new HashMap<String, Object>();
		
		List<City> countries = new ArrayList<City>();
		countries = utilService.getCitiesByCountry(countryId);
		responseBody.put("cityList", countries);
		
		return new ResponseEntity<Map<String, Object>>(responseBody, HttpStatus.OK);
	}
	
	@RequestMapping(value = "/getLanguages", method = {RequestMethod.POST, RequestMethod.GET})
	public ResponseEntity<Map<String, Object>> getLanguages(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Map<String, Object> responseBody = new HashMap<String, Object>();
		
		List<Language> languages = new ArrayList<Language>();
		languages = utilService.getLanguages();
		responseBody.put("languageList", languages);
		
		return new ResponseEntity<Map<String, Object>>(responseBody, HttpStatus.OK);
	}
	
	@RequestMapping(value = "/getSearchable", method = {RequestMethod.POST, RequestMethod.GET})
	public ResponseEntity<Map<String, Object>> getSearchable(@RequestParam(value = "page") Integer page, @RequestParam(value = "max") Integer max,
			@RequestParam(value = "searchText") String searchText, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Map<String, Object> responseBody = new HashMap<String, Object>();
		
		responseBody.put("data", utilService.getSearchable(page, max, searchText));
		
		return new ResponseEntity<Map<String, Object>>(responseBody, HttpStatus.OK);
	}
	
	@RequestMapping(value = "/getUserTypes", method = {RequestMethod.POST, RequestMethod.GET})
	public ResponseEntity<Map<String, Object>> getUserTypes(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Map<String, Object> responseBody = new HashMap<String, Object>();
		
		List<UserType> usertypes = new ArrayList<UserType>();
		usertypes  = utilService.getUserTypes();
		responseBody.put("userTypeList", usertypes);
		
		return new ResponseEntity<Map<String, Object>>(responseBody, HttpStatus.OK);
	}
	
	@RequestMapping(value = "/createTipsAndTricks", method = {RequestMethod.POST, RequestMethod.GET})
	public ResponseEntity<Map<String, Object>> createTipAndTrick(@RequestParam(value = "tipsAndTricks") String messageText, HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> responseBody = new HashMap<String, Object>();
		
		TipsAndTricks tipAndTrick = null;
		
		//creator
		User loggedUser = getLoggedUser(request);
		
		tipAndTrick = new TipsAndTricks();
		
		tipAndTrick.setContent(request.getParameter("content"));
		tipAndTrick.setCreationDate(new Date());
		tipAndTrick.setCreator(loggedUser);
		tipAndTrick.setSubject(request.getParameter("subject"));
				
		tipAndTrick = utilService.createTipAndTrick(tipAndTrick);
				
				if (tipAndTrick != null) {
						responseBody.put("tipId", tipAndTrick.getId());
					}	
		return new ResponseEntity<Map<String, Object>>(responseBody, HttpStatus.OK);
	}
	
	@RequestMapping(value = "/getAllTips", method = {RequestMethod.POST, RequestMethod.GET})
	public ModelAndView getAllTips(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView result = new ModelAndView("/tipsAndTricks.jsp");
		
		List<TipsAndTricks> tipsAndTricks = utilService.getAllTips();
			
		result.addObject("tipsAndTricks", tipsAndTricks);
		
		return result;
	}
	
	@RequestMapping(value = "/getAllTipsByType", method = {RequestMethod.POST, RequestMethod.GET})
	public ModelAndView getAllTipsByType(@RequestParam int type,HttpServletRequest request, HttpServletResponse response) {
		ModelAndView result = new ModelAndView("/tipsAndTricks.jsp");

		List<TipsAndTricks> tipsAndTricks = utilService.getTipsByType(type);
			
		result.addObject("tipsAndTricksByType", tipsAndTricks);
		
		return result;
	}
	
	@RequestMapping(value = "/getAllTipsByUser", method = {RequestMethod.POST, RequestMethod.GET})
	public ModelAndView getAllTipsByUser(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView result = new ModelAndView("/tipsAndTricks.jsp");
		
		User loggedUser = getLoggedUser(request);
		
		if (loggedUser != null) {
			List<TipsAndTricks> tipsAndTricks = utilService.getTipsByUser(loggedUser.getId());
			
			result.addObject("tipsAndTricksByUser", tipsAndTricks);
		}
		return result;
	}
	
	@RequestMapping(value = "/getAllTipsBySubject", method = {RequestMethod.POST, RequestMethod.GET})
	public ModelAndView getAllTipsBySubject(@RequestParam String subject, HttpServletRequest request, HttpServletResponse response) {
		ModelAndView result = new ModelAndView("/tipsAndTricks.jsp");
		
		User loggedUser = getLoggedUser(request);
		
		if (loggedUser != null) {
			List<TipsAndTricks> tipsAndTricks = utilService.getTipsBySubject(subject);
			
			result.addObject("tipsAndTricksBySubject", tipsAndTricks);
		}
		return result;
	}
	
	private User getLoggedUser(HttpServletRequest request) {
		HttpSession session = request.getSession();
		
		return (User) session.getAttribute(GeneralConstants.user);
	}
}